<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h2>变量作用域</h2>
</body>
<script>
    //1.全局作用域
    var a = 10;
    console.log(a)

    function fn() {
        //2.局部作用域-在函数内部定义的变量
        var a2 = 666;
        console.log('fn内部：', a)
    }

    fn()

    //3.块级作用域
    if (a > 0) {
        //var index = 0;
        let index = 0;
        const index2 = 999;
        console.log('块内部index:', index)
    }

    for (let i = 0; i < 5; i++) {
        console.log('内部i:', i)
    }


    // console.log('外部i:', i)

    console.log('index2:', index2)
</script>

</html>